window.onload = function() {
	//获取url参数
	function getUrlParam(name) {
		//构造一个含有目标参数的正则表达式对象  
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
		//匹配目标参数  
		var r = window.location.search.substr(1).match(reg);
		//返回参数值  
		if (r != null) return unescape(r[2]);
		return null;
	}
	
	var pageNum = getUrlParam("pageNum");
	alert(pageNum);
	//循环赋值
	$(document).ready(function() {
		$.ajax({
			url : "/blog/article/list?pageNum="+pageNum, //后台请求的数据
			dataType : "json", //数据格式 
			type : "get", //请求方式
			async : true, //是否异步请求
			success : function(data) { //如果请求成功，返回数据。
				$.each(data.list, function(i, ls) { //function第一个参数表示下标，第二个参数表示一维数组中的每一个数组
					if (ls.top == 1) { //==1时才置顶
						$("#contents").append(
							"<section class='article-item zoomIn article' id='content'> "
							+ "<div class='fc-flag'>" + "置顶" + "</div>"
							+ "<h5 class='title'> <span class='fc-blue'>" + "【原创】" + "</span> <a href=''>" + ls.title + "</a></h5>"
							+ "<div class='time'> </span></span> <span class='year fs-18 ml10'>" + ls.date + "</span></div>"
							+ "<div class='content' >" + ls.content + "</div>"
							+ "<div class='read-more'> " + "<a href='" + "read.html?id=" + ls.id + "'>继续阅读</a></div>"
							+ "<aside class='f-oh footer'>"
							+ " <div class='f-fl tags'> <span class='fa fa-tags fs-16'></span> <a class='tag'>" + ls.category + "</a> </div>"
							+ " <div class='f-fr'> <span class='read'> <i class='fa fa-eye fs-16'></i> <i class='num'>" + ls.viewnum + "</i></span> <span class='ml20'>"
							+ "<i class='fa fa-comments fs-16'></i><a href='javascript:void(0)' class='num fc-grey'>" + ls.connent + "</a>+ </span></div>"
							+ "</aside></section>");
					} else { //此时不置顶
						$("#contents").append(
							"<section class='article-item zoomIn article'> "
							+ "<h5 class='title'> <span class='fc-blue'>" + "【原创】" + "</span> <a href='#'>" + ls.title + "</a></h5>"
							+ "<div class='time'> </span></span> <span class='year fs-18 ml10'>" + ls.date + "</span></div>"
							+ "<div class='content' style='overflow:hidden' id='content'>" + ls.content + "</div>"
							+ "<div class='read-more'> " + "<a href='" + ls.id + "class='fc-black f-fwb'>继续阅读</a></div>"
							+ "<aside class='f-oh footer'>"
							+ " <div class='f-fl tags'> <span class='fa fa-tags fs-16'></span> <a class='tag'>" + ls.category + "</a> </div>"
							+ " <div class='f-fr'> <span class='read'> <i class='fa fa-eye fs-16'></i> <i class='num'>" + ls.viewnum + "</i></span> <span class='ml20'>"
							+ "<i class='fa fa-comments fs-16'></i><a href='javascript:void(0)' class='num fc-grey'>" + ls.connent + "</a>+ </span></div>"
							+ "</aside></section>");
					}
				});
				//获取置顶推荐，写在这里是因为不想第二次调用后台，减少MySQL压力
				$.each(data.list, function(i, ls) {
					if (ls.top == 1) {
						$("#istop").append(
							"<li><a href=>" + ls.title + "</a></li>"
						)
					}
				});
				//分页开始
				$("#pagination").append(
					"<li><a href='#'>«</a></li>"
				);
				$.each(data.navigatepageNums, function(i, ls) {
					$("#pagination").append(
						"<li><a href=/blog/article.html?pageNum=" + ls + ">" + ls + "</a></li>"
					)
				});
				$("#pagination").append(
					"<li><a href='#'>»</a></li>"
				);
			},
		})

	})
	//获取热门文章
	$(document).ready(function() {
		$.ajax({
			url : "/blog/article/list", //后台请求的数据
			dataType : "json", //数据格式 
			type : "post", //请求方式
			async : true, //是否异步请求
			success : function(data) {
				//hot文章排行榜
				$.each(data.list, function(i, ls) {
					$("#hot").append(
						"<li><a >" + ls.title + "</a></li>"
					)
				});
			}
		})
	})
}
//搜索文章函数，这里是模糊查询
function searcharticle() {
	var key = $.trim($("#searchtxt").val());
	var data = {
		"key" : key
	};
	if (key == null || key == "") {
		alert("不能为空");
		return false;
	}
	$.ajax({
		url : "/blog/article/search",
		dataType : "json", //数据格式 
		data : {
			"key" : key
		},
		type : "post", //请求方式
		async : true, //是否异步请求
		success : function(data) {
			if (data != null || data != "") {
				$('#contents').html("");
				$.each(data, function(i, ls) { //function第一个参数表示下标，第二个参数表示一维数组中的每一个数组
					//==1时才置顶
					$("#contents").append(
						"<section class='article-item zoomIn article' id='content'> "
						+ "<div class='fc-flag'>" + "置顶" + "</div>"
						+ "<h5 class='title'> <span class='fc-blue'>" + "【原创】" + "</span> <a href='#'>" + ls.title + "</a></h5>"
						+ "<div class='time'> </span></span> <span class='year fs-18 ml10'>" + ls.date + "</span></div>"
						+ "<div class='content' >" + ls.content + "</div>"
						+ "<div class='read-more'> " + "<a href='" + ls.id + "'>继续阅读</a></div>"
						+ "<aside class='f-oh footer'>"
						+ " <div class='f-fl tags'> <span class='fa fa-tags fs-16'></span> <a class='tag'>" + ls.category + "</a> </div>"
						+ " <div class='f-fr'> <span class='read'> <i class='fa fa-eye fs-16'></i> <i class='num'>" + ls.viewnum + "</i></span> <span class='ml20'>"
						+ "<i class='fa fa-comments fs-16'></i><a href='javascript:void(0)' class='num fc-grey'>" + ls.connent + "</a>+ </span></div>"
						+ "</aside></section>");
				})
			} else if (data == "[]") {
				alert("暂时查不到你想要的文章哦");
			}
		}
	})
}